package com.corpgovernment.supplier.controller;

import com.corpgovernment.api.supplier.soa.request.GetSupplierProductConfigRequestBo;
import com.corpgovernment.api.supplier.soa.request.QuerySupplierProductConfigRequestBo;
import com.corpgovernment.api.supplier.soa.response.MbSupplierProductResponse;
import com.corpgovernment.api.supplier.soa.response.QuerySupplierProductConfigResponseBo;
import com.corpgovernment.common.base.Id;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.operatelog.OperateLog;
import com.corpgovernment.common.operatelog.constant.OperateLogContant;
import com.corpgovernment.organization.vo.PageVo;
import com.corpgovernment.supplier.entity.db.MbSupplierProduct;
import com.corpgovernment.supplier.service.MbSupplierProductService;
import com.corpgovernment.supplier.util.CtripEncryptUtil;
import com.corpgovernment.supplier.vo.OperatorTypeVo;
import com.corpgovernment.supplier.vo.ProductUrlVo;
import com.corpgovernment.supplier.vo.QueryProductUrlListRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("mbSupplierProduct")
@Slf4j
public class MbSupplierProductController {
    @Autowired
    private MbSupplierProductService supplierProductService;


	@PostMapping("/getSupplierProductConfig")
	public JSONResult<MbSupplierProductResponse> getMbSupplierProductBy(@RequestBody @Valid GetSupplierProductConfigRequestBo request) {

		MbSupplierProduct products = supplierProductService.loadProductConfig(request.getSupplierCode(), request.getProductType(), request.getOperateType());
		if (Objects.isNull(products)) {
			return JSONResult.errorMsg(String.format("%s - %s - %s 未配置。", request.getSupplierCode(), request.getProductType(), request.getOperateType()));
		}
		MbSupplierProductResponse response = new MbSupplierProductResponse();
		BeanUtils.copyProperties(products, response);
		log.info("查询供应商产品结果{}---{}", products, response);
		response.setProductUrl(CtripEncryptUtil.encrypt(response.getProductUrl()));
		return JSONResult.success(response);
	}

    /**
     * 查询某供应商下某产品未配置的类型。
     * http://10.5.119.87:9526/project/494/interface/api/11792
     */
    @PostMapping("/queryOperatorTypeList")
    public JSONResult<List<OperatorTypeVo>> queryOperatorTypeList(@RequestBody @Validated(QueryProductUrlListRequest.QueryOperatorType.class) QueryProductUrlListRequest request) throws Exception {
        return JSONResult.success(supplierProductService.queryOperatorTypeList(request.getSupplierCode(), request.getProductType()));
    }


    /**
     * http://10.5.119.87:9526/project/494/interface/api/11788
     */
    @PostMapping("/getSupplierInterfaceList")
    public JSONResult<PageVo> getSupplierInterfaceList(@RequestBody QueryProductUrlListRequest req) {
        return new JSONResult<>(supplierProductService.pageQuery(req));
    }


    /**
     * 更新时只更新 url 和 userKey
     * http://10.5.119.87:9526/project/494/interface/api/11796
     */
    @PostMapping("/insertOrUpdateSupplierInterface")
    @OperateLog(OperateLogContant.SUPPLIERINSERTOREDIT)
    public JSONResult insertOrUpdate(@RequestBody ProductUrlVo p) {
        MbSupplierProduct pro = new MbSupplierProduct();
        BeanUtils.copyProperties(p, pro);
	    if (pro.getId() == null) {
		    supplierProductService.insert(pro);
	    } else {
		    supplierProductService.update(pro);
	    }
        return JSONResult.ok();
    }

    /**
     * http://10.5.119.87:9526/project/494/interface/api/11800
     */
    @PostMapping("/delete")
    @OperateLog(OperateLogContant.SUPPLIERDEL)
    public JSONResult delete(@RequestBody Id<Long> id) {
        supplierProductService.delete(id.getId());
        return JSONResult.ok();
    }


    @PostMapping("/querySupplierProductList")
    public JSONResult<QuerySupplierProductConfigResponseBo> querySupplierProductList(@RequestBody @Valid QuerySupplierProductConfigRequestBo request) {

        List<MbSupplierProduct> products = supplierProductService.loadProductConfig(request.getSupplierCodeList(), request.getProductType(), request.getOperateType());

        List<MbSupplierProductResponse> supplierProductResponses = Optional.ofNullable(products).orElse(Collections.emptyList()).stream()
                .map(p -> {
                    MbSupplierProductResponse response = new MbSupplierProductResponse();
                    BeanUtils.copyProperties(p, response);
                    response.setProductUrl(CtripEncryptUtil.encrypt(response.getProductUrl()));
                    return response;
                }).collect(Collectors.toList());
        QuerySupplierProductConfigResponseBo response = new QuerySupplierProductConfigResponseBo();
        response.setSupplierProductInfoList(supplierProductResponses);

        return JSONResult.success(response);
    }
}
